package com.ucomm.talent.activity;

import java.io.File;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.ucomm.talent.AppConfig;
import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.adapter.ImageAdapter;
import com.ucomm.talent.common.FileUtils;
import com.ucomm.talent.manager.SearchManager;
import com.ucomm.talent.widget.CustomProgressDialog;
import com.ucomm.talent.widget.MainHeaderBar;

/**
 * 总体主界面
 * Created with IntelliJ IDEA.
 * User: Vita Hao
 * Date: 13-5-21
 */
public class MainActivity extends ActivityGroup{
	
	private GridView gvBar;  
	private ImageAdapter topImgAdapter;  
	public LinearLayout container;
	private MainHeaderBar mainHeaderBar;
	private CustomProgressDialog customProgressDialog;
	private SearchManager searchManager;
	ArrayList<Object[]> buttonList=null;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		init();
	}
	@SuppressLint("UseSparseArrays")
	private void init(){
		buttonList = new ArrayList<Object[]>();
		buttonList.add(new Object[]{R.drawable.bar_home,R.string.home});
		buttonList.add(new Object[]{R.drawable.bar_sort,R.string.sort});
		buttonList.add(new Object[]{R.drawable.bar_search,R.string.search});
		buttonList.add(new Object[]{R.drawable.bar_user,R.string.user});
		gvBar = (GridView) this.findViewById(R.id.gvBar);
		gvBar.setNumColumns(buttonList.size());
		gvBar.setSelector(new ColorDrawable(Color.TRANSPARENT));
		gvBar.setGravity(Gravity.CENTER);
		gvBar.setVerticalSpacing(0);
		int width = this.getWindowManager().getDefaultDisplay().getWidth()
				/ buttonList.size();
		int height = this.getWindowManager().getDefaultDisplay().getHeight()/10;
		topImgAdapter = new ImageAdapter(this,buttonList, width, height);
		gvBar.setAdapter(topImgAdapter);
		gvBar.setOnItemClickListener(new ItemClickEvent());
		container = (LinearLayout) findViewById(R.id.Container);
        mainHeaderBar = (MainHeaderBar) findViewById(R.id.main_header_bar);
        SwitchActivity(0);

	}

    class ItemClickEvent implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			SwitchActivity(arg2);
		}
	}
	/**
	 * ???ID???????Activity
	 * @param id GridView?????????
	 */
	void SwitchActivity(int id)
	{

		container.removeAllViews();
		Window subActivity = null;
		Intent intent = null;
		if (id == 0) {
			intent = new Intent(MainActivity.this,HomeActivity.class);
			mainHeaderBar.showNormalLayout();
			intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			subActivity = getLocalActivityManager().startActivity(
					"HomeActivity", intent);
        }
		else if (id == 1){
			intent = new Intent(MainActivity.this, CategoryActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			subActivity = getLocalActivityManager().startActivity(
					"CategoryActivity", intent);

        }
		else if (id == 2){
			intent = new Intent(MainActivity.this, SearchActivityNew.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			subActivity = getLocalActivityManager().startActivity(
					"SearchActivityNew", intent);
		}
		else if (id ==3){
			intent = new Intent(MainActivity.this, UsersActivity.class);
			// mainHeaderBar.showExitBackLayout();
			intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			subActivity = getLocalActivityManager().startActivity(
					"UsersActivity", intent);
        }

		container.addView(subActivity.getDecorView(),
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	}



}
